找传奇、传世资源到传世资源站!

Quartz.NET定时任务框架实例

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

移植Quartz.Net定时任务框架,实现可配置的执行定时任务
from clipboard

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz.Impl;
using Quartz;
using DispatchingService.CustomerJob;
using DispatchingService.CustomerListener;namespace DispatchingService
{
    public class DispatchingManager
    {
        private static IScheduler scheduler;        public async static Task Init()
        {
            try
            {
                //时间轴
                StdSchedulerFactory factory = new StdSchedulerFactory();    //工厂
                scheduler = await factory.GetScheduler();                   //创建任务调度                #region SendMessageJob
                ////任务策略
                //IJobDetail jobDetail = JobBuilder.Create<SendMessageJob>()
                //    .WithIdentity("sendMessageJob", "group1")   //分组
                //    .WithDescription("这是一个sendMessage任务")   //任务描述
                //    .Build();                ////传递参数
                //jobDetail.JobDataMap.Add("student1", "张三"); //传入键值
                //jobDetail.JobDataMap.Add("student2", "李四");
                //jobDetail.JobDataMap.Add("student3", "王五");
                //jobDetail.JobDataMap.Add("year", DateTime.Now.Year);
                //jobDetail.JobDataMap.Add("count", 100);                ////时间策略
                //ITrigger trigger = TriggerBuilder.Create()
                //    .WithIdentity("sendMessageTrigger", "group1")           //分组
                //    .StartNow()                                             //立即执行一次,不是必须的(默认)
                //                                                            //.WithSimpleSchedule(w => w.WithIntervalInSeconds(5).WithRepeatCount(10))    //每5秒执行一次,总共执行10次(不包括startnow这一次)
                //    .WithSimpleSchedule(w => w.WithIntervalInSeconds(5).RepeatForever())          //一直执行   
                //    .Build();                ////传递参数
                //trigger.JobDataMap.Add("student4", "赵六");
                //trigger.JobDataMap.Add("student5", "田七");
                //trigger.JobDataMap.Add("student6", "刘八");
                //trigger.JobDataMap.Add("year", DateTime.Now.Year 1);                ////关联
                //await scheduler.ScheduleJob(jobDetail, trigger);
                #endregion                #region SayHellowJob
                //任务策略
                IJobDetail jobDetail = JobBuilder.Create<SayHelloJob>()
                    .WithIdentity("sayHelloJob", "group2")     //分组
                    .WithDescription("这是一个sayHello任务")      //任务描述
                    .Build();                //时间策略
                ITrigger trigger = TriggerBuilder.Create()
                    .WithIdentity("sayHelloTrigger", "group2")              //分组
                                                                            //.StartAt(new DateTimeOffset())                        //从某个时间点开始执行
                    .WithSimpleSchedule(w => w.WithIntervalInSeconds(5).WithRepeatCount(2))     //简单时间策略
                    .Build();                ITrigger trigger2 = TriggerBuilder.Create()
                    .WithIdentity("sayHelloTrigger2", "group2")
                    .WithCronSchedule("5/10 * * * * ?")                          //cronTrigger策略,从5开始每隔10秒执行
                    .Build();
                //监控策略
                scheduler.ListenerManager.AddJobListener(new CustomerJobListener());
                scheduler.ListenerManager.AddTriggerListener(new CustomerTriggerListener());
                //scheduler.ListenerManager.AddSchedulerListener(new CustomerSchedulerListener());                //关联
                //await scheduler.ScheduleJob(jobDetail, trigger);
                await scheduler.ScheduleJob(jobDetail, trigger2);           //添加任务                await scheduler.Start();                                    //启动任务调度线程
                #endregion
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }        public async static Task Stop()
        {
            await Task.Run(() => {
                if (scheduler != null)
                {
                    scheduler.Shutdown(true);
                }
            });
        }
    }
}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复